import pytest,json
import yaml
from Tool.tools import ApiRequestTool

#基础路径
BASE_URL = "http://127.0.0.1:8000"

#添加图书
ADD_BOOK = "/addBook/"
#删除图书
DELETE_BOOK = "/removeBook/4"
#修改图书
PUT_BOOK = "/editBook/4"
#查询图书
GET_BOOK = "/queryBook/"
@pytest.fixture(scope="module")

def api_tool():
    return ApiRequestTool(base_url=BASE_URL)

# def get_yaml_data():
#     with open(r'D:\gao1\GQzy1\addbook.yaml','r',encoding='utf-8')as f:
#         yaml_data = yaml.safe_load(f)
#         return [(i['id'],i['book'],i['status'],i['fl'])for i in yaml_data]


def get_yaml_data():
    with open(r'D:\gao1\GQzy1\putbook.yaml','r',encoding='utf-8')as f:
        yaml_data = yaml.safe_load(f)
        return [(i['id'],i['book'],i['status'],i['fl'])for i in yaml_data]

class TestGqzy1():

    print("\n===============添加图书===================\n")

    # # 添加图书(参数化)
    # @pytest.mark.parametrize("id,book,status,fl",get_yaml_data())
    # def test_post_addbook1(self,api_tool,id,book,status,fl):
    #     print("\n==========添加图书===========")
    #     addbook = {"id": id, "book": book, "status":status, "fl": fl}
    #     status_code, response_text, response = api_tool.post(path=ADD_BOOK, json=addbook)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     assert status_code == 200, f"预期 200，实际 {status_code}"
    #
    # # 添加图书(id重复添加失败)
    # def test_post_addbook2(self,api_tool):
    #     print("\n==========添加图书===========")
    #     addbook = {"id":"4","book":"老人与海","status":"1","fl":"文化"}
    #     status_code,response_text,response = api_tool.post(path=ADD_BOOK,json=addbook)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #
    #
    # # 添加图书(请求方式为put添加失败)
    # def test_post_addbook3(self, api_tool):
    #     print("\n==========添加图书===========")
    #     addbook = {"id": "4", "book": "老人与海", "status": "1", "fl": "文化"}
    #     status_code, response_text, response = api_tool.put(path=ADD_BOOK, json=addbook)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     # assert status_code == 200, f"预期 200，实际 {status_code}"
    # print("\n===============删除图书===================\n")
    #
    #
    # #删除图书(删除成功)
    # def test_delete_book1(self,api_tool):
    #     print("\n==========删除图书===========")
    #     status_code, response_text, response = api_tool.delete(path=DELETE_BOOK)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     assert status_code == 200, f"预期 200，实际 {status_code}"
    #
    #
    # #删除图书(不存在id删除失败)
    # def test_delete_book2(self,api_tool):
    #     print("\n==========删除图书===========")
    #     status_code, response_text, response = api_tool.delete(path=DELETE_BOOK)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     # assert status_code == 200, f"预期 200，实际 {status_code}"
    #
    #
    # #删除图书(请求方式错误删除失败)
    # def test_delete_book3(self,api_tool):
    #     print("\n==========删除图书===========")
    #     status_code, response_text, response = api_tool.post(path=DELETE_BOOK)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     # assert status_code == 200, f"预期 200，实际 {status_code}"
    #



    print("\n===============修改图书===================\n")
    # 修改图书(参数化)
    @pytest.mark.parametrize("id,book,status,fl", get_yaml_data())
    def test_put_putbook1(self,api_tool,id,book,status,fl):
        print("\n==========修改图书===========")
        putbook = {"id": id, "book": book, "status": status, "fl": fl}
        status_code, response_text, response = api_tool.put(path=PUT_BOOK, json=putbook)
        print(f"状态码:{status_code}")
        print(f"响应内容:{response_text}")
        assert status_code == 200, f"预期 200，实际 {status_code}"





    # # 修改图书(未填写必填参数)
    # def test_put_putbook2(self, api_tool):
    #     print("\n==========修改图书===========")
    #     putbook = {"id": "", "book": "老人与海", "status": "1", "fl": "文化"}
    #     status_code, response_text, response = api_tool.post(path=PUT_BOOK, json=putbook)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     # assert status_code == 200, f"预期 200，实际 {status_code}"
    # # 修改图书(选择不存在的id修改)
    # def test_put_putbook3(self, api_tool):
    #     print("\n==========修改图书===========")
    #     putbook = {"id": "11111", "book": "老人与海", "status": "1", "fl": "文化"}
    #     status_code, response_text, response = api_tool.post(path=PUT_BOOK, json=putbook)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     # assert status_code == 200, f"预期 200，实际 {status_code}"
    # print("\n===============查询图书===================\n")
    # #查询图书查询成功
    # def test_get_book1(self,api_tool):
    #     print("\n==========删除图书===========")
    #     status_code, response_text, response = api_tool.get(path=GET_BOOK)
    #     print(f"状态码:{status_code}")
    #     print(f"响应内容:{response_text}")
    #     assert status_code == 200, f"预期 200，实际 {status_code}"



